package com.chd.ledger_server.service.impl;

import com.chd.ledger_server.entity.ModelInstanceModels;
import com.chd.ledger_server.dao.ModelInstanceModelsDao;
import com.chd.ledger_server.service.ModelInstanceModelsService;
import com.chd.ledger_server.domain.PageDto;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.stereotype.Service;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageImpl;
import org.springframework.data.domain.PageRequest;

import javax.annotation.Resource;
import java.util.List;

/**
 * 实例模型表(ModelInstanceModels)表服务实现类
 *
 * @author makejava
 * @since 2025-07-18 19:54:18
 */
@Service("modelInstanceModelsService")
public class ModelInstanceModelsServiceImpl implements ModelInstanceModelsService {
    @Resource
    private ModelInstanceModelsDao modelInstanceModelsDao;

    /**
     * 通过ID查询单条数据
     *
     * @param instanceId 主键
     * @return 实例对象
     */
    @Override
    public ModelInstanceModels queryById(Integer instanceId) {
        return this.modelInstanceModelsDao.queryById(instanceId);
    }

    /**
     * 分页查询
     */
    @Override
    public PageInfo queryAllModelInstanceModelssByPage(PageDto pageDto) {
        //空值处理
        if (pageDto.getPage() == null || pageDto.getSize() == null) {
            pageDto.setPage(1);
            pageDto.setSize(10);
        }
        PageHelper.startPage(pageDto.getPage(), pageDto.getSize());
        List<ModelInstanceModels> list = modelInstanceModelsDao.queryAll(pageDto.getModelInstanceModels());
        PageInfo<ModelInstanceModels> pageInfo = new PageInfo<>(list);
        return pageInfo;
    }

    @Override
    public long queryId(String instanceName) {
        return modelInstanceModelsDao.queryId(instanceName);
    }

    /**
     * 新增数据
     * 链式数据插入：生成实模数据时，链式插入设备数据、实点数据
     * 编程思路：AOP，方便更新逻辑
     * 实际插入的就一个实模数据，设备数据和实点数据都是联插的
     * @param modelInstanceModels 实例对象
     * @return 实例对象
     */
    @Override
    public ModelInstanceModels insert(ModelInstanceModels modelInstanceModels) {

        this.modelInstanceModelsDao.insert(modelInstanceModels);
        return modelInstanceModels;
    }

    /**
     * 修改数据
     *
     * @param modelInstanceModels 实例对象
     * @return 实例对象
     */
    @Override
    public ModelInstanceModels update(ModelInstanceModels modelInstanceModels) {
        this.modelInstanceModelsDao.update(modelInstanceModels);
        return this.queryById(modelInstanceModels.getInstanceId());
    }

    /**
     * 通过主键删除数据
     *
     * @param instanceId 主键
     * @return 是否成功
     */
    @Override
    public boolean deleteById(Integer instanceId) {
        return this.modelInstanceModelsDao.deleteById(instanceId) > 0;
    }
}
